// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.designer.ui.process.figures;

import org.dengues.commons.ui.gef.SimpleHtmlFigure;
import org.dengues.commons.utils.ColorUtil;
import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.core.process.CompElement;
import org.dengues.ui.DenguesUiPlugin;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.RoundedRectangle;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.jface.preference.PreferenceConverter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-15 qiang.zhang $
 * 
 */
public class CompBlockFigure extends RoundedRectangle {

    private final Color background;

    private String name;

    private final SimpleHtmlFigure hint;

    /**
     * Qiang.Zhang.Adolf@gmail.com CompBlockFigure constructor comment.
     * 
     * @param compBlock
     */
    public CompBlockFigure(CompElement compBlock) {
        background = new Color(Display.getDefault(), compBlock.getColor());
        setBackgroundColor(background);
        name = compBlock.getCompDisplayName();
        hint = new SimpleHtmlFigure();
        setCornerDimensions(new Dimension(15, 15));
        setLineWidth(5);
    }

    @Override
    protected void fillShape(Graphics graphics) {
        graphics.setAntialias(SWT.ON);
        super.fillShape(graphics);
        Rectangle r = getBounds();

        graphics.setForegroundColor(ColorUtil.getAgainest(background.getRGB()));

        Font font = new Font(null, PreferenceConverter.getFontData(DenguesUiPlugin.getDefault().getPreferenceStore(),
                IDenguesPrefsConstant.DESIGNER_COLOR_FONT));
        graphics.setFont(font);
        GC gc = new GC(Display.getCurrent());
        gc.setFont(font);
        Point textExtent = gc.textExtent(name);
        gc.dispose();
        int x = r.getCenter().x - textExtent.x / 2;
        int y = r.getCenter().y - textExtent.y / 2;

        graphics.fillText(name, x, y);

    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.draw2d.RoundedRectangle#outlineShape(org.eclipse.draw2d.Graphics)
     */
    @Override
    protected void outlineShape(Graphics graphics) {
        RGB rgb = PreferenceConverter.getColor(DenguesUiPlugin.getDefault().getPreferenceStore(),
                IDenguesPrefsConstant.DESIGNER_COLOR_OUTLINE);
        graphics.setForegroundColor(new Color(null, rgb));
        super.outlineShape(graphics);
    }

    public void setHint(String hintText) {
        if (hintText.equals("")) { //$NON-NLS-1$
            setToolTip(null);
        } else {
            hint.setText(hintText);
            setToolTip(hint);
        }
    }

    /**
     * Sets the name.
     * 
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }

}
